package com.ssmdemo.controller;

import com.ssmdemo.entity.Book;
import com.ssmdemo.service.BookService;
import com.ssmdemo.utils.JsonAndObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("book")
@CrossOrigin("*")
public class BookController{


    @Autowired
    private BookService bookService;

    @GetMapping("query")
    public String query(Model model,
                        @RequestParam(value = "name",defaultValue = "")String name,
                        @RequestParam(value = "type",defaultValue = "")String type,
                        @RequestParam(value = "description",defaultValue = "")String description){
        List<Book> books = bookService.queryList(name,type,description);
        String bookList = JsonAndObjectUtil.toJson(books);
        model.addAttribute("books",bookList);
        return "index";
    }


    @RequestMapping("jsonBook")
    public @ResponseBody Book book(){
        return new Book();
    }

}